package com.ld.security.tool;

import cn.hutool.http.HttpUtil;
import com.ld.security.common.core.util.FileUtil;

import java.io.File;

/**
 * 远程加载类
 */
public class WebClassLoader extends ClassLoader {

    private String rootPath = "D:\\nacos";

    public Class<?> webLoadClass(String name) throws ClassNotFoundException {
        String[] names = name.split("/");
        String fileName = names[name.length() - 1];
        File f =  new File(rootPath+fileName);
        if(!f.exists()){
            HttpUtil.downloadFile(name,f);
        }

        byte[] bs = FileUtil.readBytes(f);
        Class<?> cla =  this.defineClass(f.getName(),bs,0,bs.length);

        return cla;
    }
}
